﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;

namespace todo.app
{
    public static class Program
    {
        [STAThread]
        public static void Main() {
            var assemblies = new Dictionary<string, Assembly>();

            AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => {
                var resourceName = string.Format("todo.app.dlls.{0}.dll", new AssemblyName(args.Name).Name);
                Trace.TraceInformation("AssemblyResolve: {0}", resourceName);

                Assembly assembly;
                if (assemblies.TryGetValue(resourceName, out assembly)) {
                    Trace.TraceInformation("  -> found in cache");
                    return assembly;
                }

                using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
                    var assemblyData = new Byte[stream.Length];
                    stream.Read(assemblyData, 0, assemblyData.Length);
                    assembly = Assembly.Load(assemblyData);
                    assemblies.Add(resourceName, assembly);
                    Trace.TraceInformation("  -> loaded from resource");

                    return assembly;
                }
            };

            App.Run();
        }
    }
}